package com.adgroup.doremon_ad;

import util.CustomDialogClass;
import util.Utility;

import com.adgroup.doremon_ad.MainActivity.waitingOpenNetwork;
import com.adgroup.doremon_ad.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class SplashScreen extends Activity {
 
        private static final int SPLASH_DISPLAY_TIME = 1500;  /* 2 seconds */
 
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.splash);
               
                 /* Create a new handler with which to start the main activity
                    and close this splash activity after SPLASH_DISPLAY_TIME has
                    elapsed. */
                
        }
        public void showMSG() {
    		runOnUiThread(new Runnable() {
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				final Dialog dialog = new CustomDialogClass(SplashScreen.this)
    						.createCustomDialog(R.layout.dialog_main,
    								SplashScreen.this, "Hi!",
    								getResources().getString(R.string.disconnect_network), false);

    				dialog.show();
    				Log.d("logTag", "noo mạng");
    				Button btnCancel = (Button) dialog
    						.findViewById(R.id.buttonCancel);
    				Button btnSetting = (Button) dialog
    						.findViewById(R.id.buttonSettings);
    				btnSetting.setOnClickListener(new View.OnClickListener() {

						@Override
    					public void onClick(View v) {
    						// TODO Auto-generated method stub
    						dialog.dismiss();
    						try {
    							handler = new Handler();
    							wait = new waitingOpenNetwork();
    							handler.postDelayed(wait, 300);

    							Intent i = new Intent(
    									Settings.ACTION_WIRELESS_SETTINGS);
    							startActivity(i);
    						} catch (Exception e) {
    							Toast.makeText(getApplicationContext(),
    									e.getMessage(), Toast.LENGTH_SHORT).show();
    						}
    					}
    				});
    				btnCancel.setOnClickListener(new View.OnClickListener() {

    					@Override
    					public void onClick(View v) {
    						finish();

    					}
    				});
    			}
    		});

    	}
        class waitingOpenNetwork implements Runnable {
    		int timewaiting = 300;
    		private boolean killMe = false;

    		@Override
    		public void run() {
    			if (Utility.checkNet(getApplicationContext()) && run) {
    				run = false;
    				handler.removeCallbacksAndMessages(null);
    				killRunnable();
    				Intent mainIntent = new Intent(SplashScreen.this,
                            MainActivity.class);
                    SplashScreen.this.startActivity(mainIntent);
                   
                    /* Finish splash activity so user cant go back to it. */
                    SplashScreen.this.finish();
                   
                    /* Apply our splash exit (fade out) and main
                       entry (fade in) animation transitions. */
                    overridePendingTransition(R.anim.in,
                            R.anim.out);
    			}
    			// Toast.makeText(getApplicationContext(), "waiting",
    			// Toast.LENGTH_SHORT).show();
    			handler.postDelayed(wait, timewaiting);
    			if (killMe)
    				return;

    		}

    		private void killRunnable() {
    			killMe = true;
    		}
    	}
        boolean run = true;

		private waitingOpenNetwork wait;

		private Handler handler;
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
             if(Utility.checkNet(getApplicationContext())){
            /* Create an intent that will start the main activity. */
            Intent mainIntent = new Intent(SplashScreen.this,MainActivity.class);
            SplashScreen.this.startActivity(mainIntent);
           
            /* Finish splash activity so user cant go back to it. */
            SplashScreen.this.finish();
           
            /* Apply our splash exit (fade out) and main
               entry (fade in) animation transitions. */
            overridePendingTransition(R.anim.in,
                    R.anim.out);
             }else{
            	 showMSG();
             }
    }
}, SPLASH_DISPLAY_TIME);
}
}